﻿using System;
using System.Windows;
using ControleRepresentanteSync.Application.DynamicSync;
using ControleRepresentanteSync.View.Wizard;

namespace ControleRepresentanteSync.View.DynamicSync
{
    abstract class DynamicSyncContentViewModel : WizardPageViewModelBase, IDynamicSyncContentViewModel
    {
        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(bool), typeof(DynamicSyncContentViewModel), new PropertyMetadata(OnIsCheckedChanged));

        private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((DynamicSyncContentViewModel)d).FireIsCheckedChanged();
        }

        protected static void OnChanged<T>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<DynamicSyncContentViewModel, T> setValue)
            where T : class
        {
            DynamicSyncContentViewModel contentViewModel = (DynamicSyncContentViewModel)d;
            T oldValue = e.OldValue as T;
            if (e.NewValue == null && oldValue != null)
            {
                setValue(contentViewModel, oldValue);
            }
            contentViewModel.FireChanged();
        }

        protected DynamicSyncContentViewModel(string title) 
            : base(title, String.Empty)
        {
        }

        public event EventHandler Changed;
        public event EventHandler IsCheckedChanged;

        private void FireIsCheckedChanged()
        {
            if (IsCheckedChanged != null)
                IsCheckedChanged(this, EventArgs.Empty);
        }

        private void FireChanged()
        {
            if (Changed != null)
                Changed(this, EventArgs.Empty);
        }

        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }
    }
}
